﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GMap.NET;

namespace MyPoints.Model
{
  public class GpxModel
  {
    private gpxType inner = new gpxType();
    private List<wptType> wpts = new List<wptType>();

    #region Properties

    public string[] TypeNames
    {
      get
      {
        var ret =
          wpts.GroupBy(q => q.Category).Select(q => q.Key).ToArray();
        return ret;
      }
    }

    public Dictionary<string, wptType[]> TypeSets
    {
      get
      {
        var ret =
          wpts.GroupBy(q => q.Category).ToDictionary(q=>q.Key, q=>q.ToArray());
        return ret;
      }
    }

    public metadataType Metadata
    {
      get
      {
        return inner.metadata;
      }
      set
      {
        if (value == null)
          throw new ArgumentNullException("Argument \"value\" is null.");

        inner.metadata = value;
      }
    }

    public wptType[] Wpts
    {
      get
      {
        return wpts.ToArray();
      }
    }

    #endregion Properties

    public GpxModel(gpxType xmlData) : this()
    {
      this.inner = xmlData;

      inner.wpt.ToList().ForEach(q => AddWpt(q));
    }

    public GpxModel() { }
    
    public gpxType ComposeAndGetXmlObject()
    {
      inner.wpt = wpts.ToArray();
      return inner;
    }

    public void AddWpt(wptType wpt)
    {
      wpts.Add(wpt);
    }

    public void RemoveWpt(wptType wpt)
    {
      wpts.Remove(wpt);
    }

    internal gpxType ToXmlObject()
    {
      inner.creator = "https://mypoints.codeplex.com/";
      inner.wpt = wpts.ToArray();

      return inner;
    }
  }
}
